package jp.bsws.action.master;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import jp.bsws.bean.GenkinShutsunoBean;
import jp.bsws.bean.HosyuShubetsuBean;
import jp.bsws.bean.KeyValueBean;
import jp.bsws.common.BSWSConstants;
import jp.bsws.common.BSWSSession;
import jp.bsws.common.BSWSUtils;
import jp.bsws.common.DBUtils;
import jp.bsws.dao.BSWSDao;
import jp.bsws.dao.HosyuShubetsuDao;
import jp.bsws.dao.UriageShubetsuDao;
import jp.bsws.model.HosyuShubetsuModel;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/*
 * メインメニューアクションクラス
 */
public class HosyuShubetsuMasterAction extends ActionSupport {
	private static final long serialVersionUID = 1L;

	private BSWSSession bSWSSession;

	public String getLoginJugyoinKnj() { return bSWSSession.getStrJugyoinKnj(); }

	private List<HosyuShubetsuBean> hosyuShubetsuBeanList;
	private Integer intTaisyoSeq;

	public void validateUpdateRaitenYoin() {

		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);

		//必須チェック
//		for(int i =0; i < raitenYoinBeanList.size(); i++){
//			RaitenYoinBean raitenYoinBean = raitenYoinBeanList.get(i);
//			if(intTaisyoSeq == raitenYoinBean.getSeq() && raitenYoinBean.isChkEdit()){
//				if (BSWSUtils.isBlankNull(raitenYoinBean.getStrRaitenYoinKnj())) {
//					addFieldError("raitenYoinBeanList[" + i + "].strRaitenYoinKnj", (i + 1) + "行目の来店要因名称を入力して下さい。");
//				}
//				break;
//			}
//		}
	}

	private void init() throws Exception{
		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);
	}

	@Override
	public String execute() throws Exception{

		//初期設定
		init();

		Connection con = DBUtils.getConnection();
		try{
			//画面表示内容の設定
			hyojiJunbi(con);
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	/**
	 * 報酬種別追加
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String add() throws Exception{

		init();

		UriageShubetsuDao uriageShubetsuDao = UriageShubetsuDao.getInstance();

		Connection con = DBUtils.getConnection();
		try{
			for(HosyuShubetsuBean hosyuShubetsuBean : this.hosyuShubetsuBeanList) {
				hosyuShubetsuBean.setStrGroupCod(bSWSSession.getStrGroupCod());
				List<KeyValueBean> tmpList = uriageShubetsuDao.getUriageShubetsuList(con, bSWSSession.getStrGroupCod(), null);
				tmpList.add(new KeyValueBean(BSWSConstants.C_HOSYU_NYURYOKU_COD,BSWSConstants.C_HOSYU_NYURYOKU_KNJ));
				hosyuShubetsuBean.setKeisanMotoList(tmpList);
				hosyuShubetsuBean.setHyojiKbnList(BSWSConstants.hyojiKbnList);
			}

			//報酬種別行追加
			HosyuShubetsuBean hosyuShubetsuBean = new HosyuShubetsuBean();
			hosyuShubetsuBean.setStrGroupCod(bSWSSession.getStrGroupCod());
			List<KeyValueBean> tmpList = uriageShubetsuDao.getUriageShubetsuList(con, bSWSSession.getStrGroupCod(), null);
			tmpList.add(new KeyValueBean(BSWSConstants.C_HOSYU_NYURYOKU_COD,BSWSConstants.C_HOSYU_NYURYOKU_KNJ));
			hosyuShubetsuBean.setKeisanMotoList(tmpList);
			hosyuShubetsuBean.setHyojiKbnList(BSWSConstants.hyojiKbnList);
			this.hosyuShubetsuBeanList.add(hosyuShubetsuBean);
		}catch(SQLException se){
			con.rollback();
			throw se;
		}finally{
			con.close();
		}


		return SUCCESS;
	}

	public String updateHosyuShubetsu() throws Exception{

		HosyuShubetsuDao hosyuShubetsuDao = HosyuShubetsuDao.getInstance();

		//初期設定
		init();

		Connection con = DBUtils.getConnection();
		try{

			//報酬種別情報更新対象取得
			for(HosyuShubetsuBean hosyuShubetsuBean : hosyuShubetsuBeanList) {
				int cnt = 0;
				if(hosyuShubetsuBean.getSeq() != null){
					//情報更新
					cnt = hosyuShubetsuDao.updateHosyuShubetsu(con, bSWSSession, hosyuShubetsuBean);
				}else{
					if(BSWSUtils.isNotBlankNull(hosyuShubetsuBean.getStrHosyuShubetsuKnj())
							&& BSWSUtils.isNotBlankNull(hosyuShubetsuBean.getStrHosyuShubetsuRyakuKnj())
							&& BSWSUtils.isNotBlankNull(hosyuShubetsuBean.getStrHosyuShubetsuHyojiJun())
							&& BSWSUtils.isNotBlankNull(hosyuShubetsuBean.getStrHosyuShubetsuHyojiFlg())
							&& BSWSUtils.isNotBlankNull(hosyuShubetsuBean.getStrKeisanMotoCod())
					){
						//情報追加
						cnt = hosyuShubetsuDao.insertHosyuShubetsu(con,  null, bSWSSession, hosyuShubetsuBean);
					}
				}
				if(cnt == 0){
					//処理なし
				}
			}

			//画面表示内容の設定
			hyojiJunbi(con);

			con.commit();
		}catch(SQLException se){
			con.rollback();
			throw se;
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	public String deleteHosyuShubetsu() throws Exception{

		BSWSDao bSWSDao = BSWSDao.getInstance();

		//初期設定
		init();

		Connection con = DBUtils.getConnection();
		try{
			//報酬種別削除
			int cnt = bSWSDao.deleteRow(con, intTaisyoSeq, " M_HOSYU_SHUBETSU ");
			if(cnt == 0){
				//処理なし
			}

			//画面表示内容の設定
			hyojiJunbi(con);

			con.commit();
		}catch(SQLException se){
			con.rollback();
			throw se;
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	private void hyojiJunbi(Connection con) throws Exception{

		UriageShubetsuDao uriageShubetsuDao = UriageShubetsuDao.getInstance();
		//報酬種別マスタ情報を取得
		getHosyuShubetsuJoho(con);

		//報酬種別行追加
		HosyuShubetsuBean hosyuShubetsuBean = new HosyuShubetsuBean();
		hosyuShubetsuBean.setStrGroupCod(bSWSSession.getStrGroupCod());
		List<KeyValueBean> tmpList = uriageShubetsuDao.getUriageShubetsuList(con, bSWSSession.getStrGroupCod(), null);
		tmpList.add(new KeyValueBean(BSWSConstants.C_HOSYU_NYURYOKU_COD,BSWSConstants.C_HOSYU_NYURYOKU_KNJ));
		hosyuShubetsuBean.setKeisanMotoList(tmpList);
		hosyuShubetsuBean.setHyojiKbnList(BSWSConstants.hyojiKbnList);
		hosyuShubetsuBeanList.add(hosyuShubetsuBean);
	}

	private void getHosyuShubetsuJoho(Connection con) throws Exception{

		HosyuShubetsuDao hosyuShubetsuDao = HosyuShubetsuDao.getInstance();
		UriageShubetsuDao uriageShubetsuDao = UriageShubetsuDao.getInstance();
		this.hosyuShubetsuBeanList = new ArrayList<HosyuShubetsuBean>();
		this.hosyuShubetsuBeanList.clear();

		//報酬種別マスタ情報を取得
		List<HosyuShubetsuModel> hosyuShubetsuList = hosyuShubetsuDao.getHosyuShubetsuJoho(con, null, bSWSSession.getStrGroupCod(), null);
		for(int i = 0; i < hosyuShubetsuList.size(); i++){
			HosyuShubetsuBean hosyuShubetsuBean = new HosyuShubetsuBean();
			hosyuShubetsuBean.setSeq(hosyuShubetsuList.get(i).getSeq());
			hosyuShubetsuBean.setStrGroupCod(bSWSSession.getStrGroupCod());
			hosyuShubetsuBean.setStrHosyuShubetsuKnj(hosyuShubetsuList.get(i).getStrHosyuShubetsuKnj());
			hosyuShubetsuBean.setStrHosyuShubetsuRyakuKnj(hosyuShubetsuList.get(i).getStrHosyuShubetsuRyakuKnj());
			hosyuShubetsuBean.setStrHosyuShubetsuHyojiJun(String.valueOf(hosyuShubetsuList.get(i).getIntHosyuShubetsuHyojiJun()));
			hosyuShubetsuBean.setStrHosyuShubetsuHyojiFlg(hosyuShubetsuList.get(i).getStrHosyuShubetsuHyojiFlg());
			hosyuShubetsuBean.setStrKeisanMotoCod(hosyuShubetsuList.get(i).getStrKeisanMotoCod());
			List<KeyValueBean> tmpList = uriageShubetsuDao.getUriageShubetsuList(con, bSWSSession.getStrGroupCod(), null);
			tmpList.add(new KeyValueBean(BSWSConstants.C_HOSYU_NYURYOKU_COD,BSWSConstants.C_HOSYU_NYURYOKU_KNJ));
			hosyuShubetsuBean.setKeisanMotoList(tmpList);
			hosyuShubetsuBean.setHyojiKbnList(BSWSConstants.hyojiKbnList);
			this.hosyuShubetsuBeanList.add(hosyuShubetsuBean);
		}
	}

	public BSWSSession getbSWSSession() {
		return bSWSSession;
	}

	public void setbSWSSession(BSWSSession bSWSSession) {
		this.bSWSSession = bSWSSession;
	}

	public List<HosyuShubetsuBean> getHosyuShubetsuBeanList() {
		return hosyuShubetsuBeanList;
	}

	public void setHosyuShubetsuBeanList(
			List<HosyuShubetsuBean> hosyuShubetsuBeanList) {
		this.hosyuShubetsuBeanList = hosyuShubetsuBeanList;
	}

	public Integer getIntTaisyoSeq() {
		return intTaisyoSeq;
	}

	public void setIntTaisyoSeq(Integer intTaisyoSeq) {
		this.intTaisyoSeq = intTaisyoSeq;
	}

}